package com.aznest.mapper;

import com.aznest.entity.Orders;
import com.aznest.vo.OrdersVO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 操作orders相关数据接口
*/
@Mapper
public interface OrdersMapper {

    /**
      * 新增
    */
    int insert(Orders orders);

    /**
      * 删除
    */
    int deleteById(Integer id);

    /**
      * 修改
    */
    int updateById(Orders orders);

    /**
      * 根据ID查询
    */
    Orders selectById(Integer id);

    /**
      * 查询所有
    */
    List<OrdersVO> selectAll(Orders orders);

    @Select("select * from orders where order_id = #{orderId}")
    Orders selectByOrderId(String orderId);

    List<Orders> selectByHotelId(Integer id);


    Integer selectByTypeAndTime(Integer typeId, String inTime, String outTime);

    @Select("select count(*) from orders\n" +
            "where YEAR(time) = YEAR(CURRENT_DATE)\n" +
            "AND MONTH(time) = MONTH(CURRENT_DATE)")
    Integer selectCurMonthOrdersCnt();



    @Select("select SUM(price) from orders\n" +
            "where DATE(time) = DATE(CURRENT_DATE)\n" +
            "and `status` IN ('待入住','已退房');")
    Double selectTodayIncome();

    @Select("SELECT \n" +
            "    YEAR(time) AS syear,\n" +
            "    MONTH(time) AS smonth,\n" +
            "    SUM(price) AS total_sales\n" +
            "FROM \n" +
            "    orders\n" +
            "WHERE \n" +
            "    time >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)\n" +
            "\t\tand hotel_id = #{hotelId}\n" +
            "GROUP BY \n" +
            "    YEAR(time), MONTH(time)\n" +
            "ORDER BY \n" +
            "    syear, smonth;")
    List<Map<String, Object>> selectCurYearIncome(Integer hotelId);

    @Select("select count(*) from orders where type_id = #{typeId}")
    Integer selectTypeOrderNum(Integer id);

    @MapKey("hotelId")
    List<Map<String, Object>> selectHotelAndOrdersNum();

    @MapKey("hotelId")
    List<Map<String, Object>> selectHotelIncomeTopFive();

    @MapKey("date")
    List<Map<String, Object>> curMonthOrders(Integer id);

    @Update("update orders set status = #{status} where order_id = #{orderId}")
    void updateStatus(String orderId, String status);
}
